package com.yzy.util.wx;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.yzy.config.redis.RedisUtil;
import com.yzy.properties.GlobalProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 推送微信消息
 * 
 * @Date: 2022/11/21
 **/
@Slf4j
@Component
public class PutMessageUtil {
    @Resource
    private RedisUtil redisUtil;

    @Resource
    private GlobalProperties globalProperties;

    /**
     * 获取Access token,2小时过期
     */
    private String getAppToken() {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + globalProperties.getWxAppId() + "&secret=" + globalProperties.getWxAppIdSecret();
        String response = cn.hutool.http.HttpRequest.post(url).execute().body();
        String accessToken = JSONObject.parseObject(response).getString("access_token");
        // access_token的有效期目前为2个小时，需定时刷新，重复获取将导致上次获取的access_token失效。
        redisUtil.set("accessToken", accessToken, 120);
        return accessToken;
    }

    public static void main(String[] args) {
        String openId = "oqpOs6QoXlH0e2nYJ5l4o3c7AyPs";
        String url1 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa3289be670237d43&secret=c574c016939f195c5c6eab8a8f1185b3";
        String response = cn.hutool.http.HttpRequest.post(url1).execute().body();
        String accessToken = JSONObject.parseObject(response).getString("access_token");
        // 请求地址
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        // 消息内容
        Map<String, WeChatTemplateMsg> sendMag = new HashMap<>(10);
        sendMag.put("first", new WeChatTemplateMsg("你好， 演示项目设备告警详情如下"));
        sendMag.put("keyword1", new WeChatTemplateMsg(LocalDateTime.now()));
        sendMag.put("keyword2", new WeChatTemplateMsg("称重曲线异常"));
        sendMag.put("keyword3", new WeChatTemplateMsg("称重仪表"));
        sendMag.put("keyword4", new WeChatTemplateMsg("XM3000001"));
        sendMag.put("keyword5", new WeChatTemplateMsg("紧急消息"));
        sendMag.put("remark", new WeChatTemplateMsg("请及时查看"));
        // json 数据
        Map<String, Object> sendBody = new HashMap<>();
        //  点击模板信息跳转地址
        sendBody.put("url", "http://yz.e-zoom.cloud/h5index.html#/pages/home/msgList");
        // 顶色
        sendBody.put("topcolor", "#FF0000");
        sendBody.put("data", sendMag);
        // 公众号的模板id
        String templateId = "V69eAvi7cGA-efCmsPyUTSjGDhYhcpQqLoXhR2A45r4";
        sendBody.put("template_id", templateId);
        // 开始推送
        sendBody.put("touser", openId);
        String response1 = HttpRequest.post(url).body(JSONObject.toJSONString(sendBody)).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(response1);
        log.info("messageCode : " + jsonObject.getString("errcode") + ", msgId: " + jsonObject.getString("msgid"));
    }

}
